<?php
/////////////////////////////////////////////////////////////////////////////
// 这个文件是 网城创想多用户商城 项目的一部分
//
// Copyright (c) 2007 - 2008 www.shopnc.net 
//
// 要查看完整的版权信息和许可信息，请查看源代码中附带的 COPYRIGHT 文件，
// 或者访问 http://www.shopnc.net/ 获得详细信息。
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : score.php   FILE_PATH : D:\webroot\mulitishop\member\score.php
 * ....评价管理
 *
 * @copyright Copyright (c) 2007 - 2007 www.shopnc.net 
 * @author 网城创想多用户商城开发团队 php_netproject@yahoo.com.cn
 * @package 
 * @subpackage 
 * @version Thu Oct 18 14:03:51 CST 2007
 */
require ("../global.inc.php");

class OwnScoreManage extends CommonFrameWork{
	/**
	 * 评价对象
	 *
	 * @var obj
	 */
	var $obj_score;
	/**
	 * 验证对象
	 *
	 * @var obj
	 */
	var $objvalidate;
	/**
	 * 商品订单对象
	 *
	 * @var obj
	 */
	var $obj_product_order;
	/**
	 * 会员对象
	 *
	 * @var obj
	 */
	var $obj_member;
	/**
	 * 分页对象
	 *
	 * @var obj
	 */
	var $obj_page;
	/**
	 * 商品对象
	 *
	 * @var obj
	 */
	var $obj_product;
	/**
	 * 网站提醒对象
	 *
	 * @var obj
	 */
	var $obj_remind;
	
	function main(){
		/**
		 * 创建评价对象
		 */
		if (!is_object($this->obj_score)){
			require_once("score.class.php");
			$this->obj_score = new ScoreClass();
		}
		/**
		 * 创建验证对象
		 */
		if (!is_object($this->objvalidate)){
			require_once("commonvalidate.class.php");
			$this->objvalidate = new CommonValidate();
		}
		/**
		 * 创建商品订单对象
		 */
		if (!is_object($this->obj_product_order)){
			require_once("order.class.php");
			$this->obj_product_order = new ProductOrderClass();
		}
		/**
		 * 创建会员对象
		 */
		if (!is_object($this->obj_member)){
			require_once("member.class.php");
			$this->obj_member = new MemberClass();
		}
		/**
		 * 设置模板路径
		 */
		$this->setsubtemplates("member");

		/**
		 * 语言包
		 */
		$this->getlang("product");
		
		$this->memberMenuModule();
		
		switch($this->_input['action']){
			case "add":
				$this->_addScore();
				break;
			case "modify":
				$this->_modiScore();
				break;
			case "save":
				$this->_saveScore();
				break;
			default:
				$this->_getScore();
		}
	}
	
	/**
	 * 进行评价
	 *
	 */
	function _addScore(){
		/**
		 * 验证参数
		 */
		$this->objvalidate->validateparam = array(
		array("input"=>$this->_input["orderid"],"require"=>"true","message"=>"错误的地址"),
		array("input"=>$this->_input["type"],"require"=>"true","message"=>"错误的地址"),
		);
		
		/**
		 * 被评价的商品信息
		 */
		$product_order_array = $this->obj_product_order->getOneOrder($this->_input["orderid"]);
		
		$member_array = array();
		
		if($this->_input["type"] == "bought"){
			$page_title = "买家评价";
			$condition['id'] = $product_order_array['seller_id'];
			$member_array = $this->obj_member->getMemberInfo($condition);
			$score_genre = "b";
		}else if($this->_input["type"] == "sold"){
			$page_title = "卖家评价";
			$condition['id'] = $product_order_array['buyer_id'];
			$member_array = $this->obj_member->getMemberInfo($condition);
			$score_genre = "s";
		}

		$product_order_array['member_name'] = $member_array['login_name'];
		$product_order_array['genre'] = $score_genre;
		
		
		/**
		 * 页面输出
		 */
		$this->output('product_array',$product_order_array);
		$this->output('rdoScore',Common::showForm_Radio("score","",$this->_b_config['grade_score'],'1'));
		$this->output('page_title',$page_title);
		$this->showpage('own_score.add');
	}
	
	/**
	 * 将评价存放到数据库中
	 *
	 */
	function _saveScore(){
		/**
		 * 验证参数
		 */
		$this->objvalidate->validateparam = array(
		array("input"=>$this->_input["orderid"],"require"=>"true","message"=>"错误的地址"),
		array("input"=>$this->_input["genre"],"require"=>"true","message"=>"错误的地址"),
		);
		
		/**
		 * 被评价的商品信息
		 */
		$product_order_array = $this->obj_product_order->getOneOrder($this->_input["orderid"]);
		
		if($this->_input["genre"] == "b"){
			$this->_input['memberid'] = $product_order_array['buyer_id'];
			$this->_input['gradememberid'] = $product_order_array['seller_id'];
			$this->_input["genre"] = "s";
		}else if($this->_input["genre"] == "s"){
			$this->_input['memberid'] = $product_order_array['seller_id'];
			$this->_input['gradememberid'] = $product_order_array['buyer_id'];
			$this->_input["genre"] = "b";
		}
		/**
		 * 存放评价并更改订单的评价状态
		 */
		if($this->obj_score->addScore($this->_input) == true){
			$this->obj_product_order->updateProductOrderCommentState($this->_input["orderid"],$this->_input["genre"]);
			$this->obj_member->updateMemberScore($this->_input["genre"],$this->_input['score'],$this->_input['gradememberid']);
		}
		
		/**
		 * 网站提醒操作
		 */
		if (!is_object($this->obj_remind)){
			require_once('remind.class.php');
			$this->obj_remind = new RemindClass();
		}
		$condition['id'] = $this->_input['gradememberid'];
		$member_array = $this->obj_member->getMemberInfo($condition);

		$value_array = array();
		$value_array['username'] = $member_array['login_name'];
		$value_array['product_name'] = $product_order_array['p_name'];
		$this->obj_remind->setMessageOrMail('score_have_notice','score_have_notice',$value_array,$member_array['login_name'],$this->_configinfo);
		
		$this->redirectPath("succ","member/own_main.php", "评价成功");
	}
	
	/**
	 * 评价管理 
	 */
	function _getScore(){
		/**
		 * 创建分页对象
		 */
		if (!is_object($this->obj_page)){
			require_once("commonpage.class.php");
			$this->obj_page = new CommonPage();
		}
		$this->obj_page->pagebarnum(20);    //每页20条记录
		/**
		 * 取会员资料信息
		 */
		$condition_member['id'] = $_SESSION['s_login']['id'];
		$member_array = $this->obj_member->getMemberInfo($condition_member);
		/**
		 * 取会员所有评价，包括卖家和买家的
		 */
		/*买家*/
		$buy_score = $this->obj_score->getScore(&$this->obj_page,$_SESSION['s_login']['id']);
		if (is_array($buy_score)){
			foreach ($buy_score as $k => $v){/*先按照好评、中评、差评区分*/
				if ($v['score'] == '1'){/*好评*/
					$buy_array['good'][] = $v;
				}elseif ($v['score'] == '0'){/*中评*/
					$buy_array['normal'][] = $v;
				}elseif ($v['score'] == '-1'){/*差评*/
					$buy_array['bad'][] = $v;
				}
			}
			if (is_array($buy_array)){
				/*每组评价按照时间段分*/
				foreach ($buy_array as $k => $v){
					$week=0;$month=0;$halfyear=0;$year=0;
					foreach ($v as $k2 => $v2){
						if ($v2['pubtime'] >= (time()-7*60*60*24)){
							$week++;
						}
						if ($v2['pubtime'] >= (time()-30*60*60*24)){
							$month++;
						}
						if ($v2['pubtime'] >= (time()-6*30*60*60*24)){
							$halfyear++;
						}
						if ($v2['pubtime'] <= (time()-6*30*60*60*24)){
							$year++;
						}
					}
					$buy_count[$k] = array('week'=>$week,'month'=>$month,'halfyear'=>$halfyear,'year'=>$year);
				}
			}
		}
		/*卖家*/
		$sale_score = $this->obj_score->getScore(&$this->obj_page,$_SESSION['s_login']['id'],'s','to');
		if (is_array($sale_score)){
			foreach ($sale_score as $k => $v){/*先按照好评、中评、差评区分*/
				if ($v['score'] == '1'){/*好评*/
					$sale_array['good'][] = $v;
				}elseif ($v['score'] == '0'){/*中评*/
					$sale_array['normal'][] = $v;
				}elseif ($v['score'] == '-1'){/*差评*/
					$sale_array['bad'][] = $v;
				}
			}
			if (is_array($sale_array)){
				/*每组评价按照时间段分*/
				foreach ($sale_array as $k => $v){
					$week=0;$month=0;$halfyear=0;$year=0;
					foreach ($v as $k2 => $v2){
						if ($v2['pubtime'] >= (time()-7*60*60*24)){
							$week++;
						}
						if ($v2['pubtime'] >= (time()-30*60*60*24)){
							$month++;
						}
						if ($v2['pubtime'] >= (time()-6*30*60*60*24)){
							$halfyear++;
						}
						if ($v2['pubtime'] <= (time()-6*30*60*60*24)){
							$year++;
						}
					}
					$sale_count[$k] = array('week'=>$week,'month'=>$month,'halfyear'=>$halfyear,'year'=>$year);
				}
			}
		}
		/*判断评价列表中需要显示的内容*/
		if ($this->_input['time'] != "" || $this->_input['score'] != "" || $this->_input['genre'] != ""){
			
			if ($this->_input['genre'] == 'b'){
				$scoretype = "from";
			}elseif ($this->_input['genre'] == 's'){
				$scoretype = "to";
			}elseif ($this->_input['genre'] == 'all'){
				$scoretype = "all";
			}
			$score_array = $this->obj_score->getScore(&$this->obj_page,$_SESSION['s_login']['id'],$this->_input['genre'],$scoretype,$this->_input['score'],$this->_input['time']);
		}else {/*默认为收到的所有好评*/
			$score_array = $buy_score;
		}
		$pagelist = $this->obj_page->show(1);      //分页显示
		
		/**
		 * 买家卖家好评率
		 */
		$sale_percent = @number_format(($sale_count['good']['halfyear']+$sale_count['good']['year']-$sale_count['bad']['halfyear']+$sale_count['bad']['year'])/($sale_count['good']['halfyear']+$sale_count['good']['year']),1)*100;
		$buy_percent = @number_format(($buy_count['good']['halfyear']+$buy_count['good']['year']-$buy_count['bad']['halfyear']+$buy_count['bad']['year'])/($buy_count['good']['halfyear']+$buy_count['good']['year']),1)*100;
		
		/**
		 * 页面输出
		 */
		$this->output('member_array',$member_array);/*会员资料*/
		$this->output('sale_count',$sale_count);/*卖家数量统计*/
		$this->output('buy_count',$buy_count);/*买家数量统计*/
		$this->output('score_array',$score_array);/*评价列表*/
		$this->output('pagelist',$pagelist);/*输出消息分页*/
		$this->output('time',$this->_input['time']);/*评价条件-时间*/
		$this->output('genre',$this->_input['genre']);/*评价条件-买家或卖家*/
		$this->output('score',$this->_input['score']);/*评价条件-评价等级，好评-中评-差评*/
		$this->output('sale_percent',$sale_percent);/*卖家好评率*/
		$this->output('buy_percent',$buy_percent);/*买家好评率*/
		$this->showpage('own_score.manage');
	}

}
$score_manage = new OwnScoreManage();
$score_manage->main();
unset($score_manage);
?>